﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace LibCom
{
    public class Testen
    {
        private static Random rnd=new Random();
        static void setRandomOrientation(cardType ct)
        {
            ct.openings = new cardTypeOpenings();
            ct.openings.top=rnd.Next(0, 2) == 0;
            ct.openings.bottom=rnd.Next(0, 2) == 0;
            ct.openings.left=rnd.Next(0, 2) == 0;
            ct.openings.right=rnd.Next(0, 2) == 0;
            ct.treasure=null;
            ct.pin = new int[0];
        }

        static void distributePlayers(cardType[][] Board)
        {
            int x, y;
            for (int i = 0; i < 4; i++)
            {
                y = rnd.Next(0, 7);
                x = rnd.Next(0, 7);
                List<int> tmp= Board[y][x].pin.ToList();
                tmp.Add(i);
                Board[y][x].pin=tmp.ToArray();
            }
        }

        static void distributeTreasure(cardType[][] Board)
        {
            List<cardType> ls = new List<cardType>();
            foreach (var v in Board)
            {
                foreach (var u in v)
                {
                    ls.Add(u);
                }
            }
            for (int i = 0; i < 28; i++)
            {
                int n = rnd.Next(0, ls.Count);
                ls[n].treasure = (treasureType)i;
                ls.Remove(ls[n]);
            }
        }

        static cardType[][] getBoard()
        {
            int i,j;
            cardType[][] ret = new cardType[7][];
            for (i=0; i<7; i++)
            {
                ret[i]=new cardType[7];
                for (j = 0; j < 7; j++) { ret[i][j] = new cardType(); setRandomOrientation(ret[i][j]); }
            }
            distributePlayers(ret);
            distributeTreasure(ret);
            return ret;
        }

        static void dispBoard(cardType[][] Board)
        {
            int i, j;
            for (i=0; i<7; i++)
            {
                for (j=0; j<7;j++)
                {
                    Console.Write( Board[i][j].pin.Length>0? 'X' : ' '  );
                }
                Console.WriteLine();
            }
        }

        [DllImport("Library.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static void printTreasures(Treasure t);

        public static void Main(string[] args)
        {
            cardType[][] Board = getBoard();
            cardType ins= new cardType();
            setRandomOrientation(ins);
            int[] einschub = { 6, 3 };
            int[] Players = { 0,1,2,3 };
            treasureType gl= treasureType.sym20;
            dispBoard(Board);
            Zug turn=Connecting.getZug(Board, ins, gl, einschub, Players);
            Console.WriteLine("Done");
            //Console.ReadKey();
        }
    }
}
